<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=UTF-8" session="false" deferredSyntaxAllowedAsLiteral="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加场次</title>
<link href="${pageContext.request.contextPath}/page/admin/static/h-ui/css/H-ui.min.css" rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath}/page/admin/static/h-ui.admin/css/H-ui.login.css" rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath}/page/admin/static/h-ui.admin/css/style.css" rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath}/page/admin/lib/Hui-iconfont/1.0.8/iconfont.css" rel="stylesheet" type="text/css" />
<script src="${pageContext.request.contextPath}/page/admin/lib/jquery/1.9.1/jquery.min.js"></script> 
<script src="${pageContext.request.contextPath}/static/lib/layer/2.4/layer.js"></script>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/jquery.sPage.css">
<script src="${pageContext.request.contextPath}/static/js/jquery.sPage.js"></script>
</head>
<body>
<div class="page-container">
	<div class="mt-20">
	<form class="form form-horizontal" id="form-admin-add">
	<div class="row cl">
		<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>专业：</label>
		<div class="formControls col-xs-8 col-sm-9">
			<select id="majorSelect">
				<option value="0">请选择专业</option>
				<c:forEach items="${majorList}" var="major" varStatus="status">
					<option value="${status.count}">${major.majorName}</option>
				</c:forEach>
			</select>
		</div>
	</div>	
	<div class="row cl">
		<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>班级：</label>
		<div class="formControls col-xs-8 col-sm-9">
			<select id="classesSelect">
				<option value="0">请选择班级</option>
			</select>
		</div>
	</div>	
	<div class="row cl">
		<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>科目：</label>
		<div class="formControls col-xs-8 col-sm-9">
			<select id="subjectSelect">
				<option value="0">请选择科目</option>
				<c:forEach items="${subjectList}" var="subject" varStatus="status">
					<option value="${status.count}">${subject.subjectName}</option>
				</c:forEach>
			</select>
		</div>
	</div>
	<div class="row cl">
		<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>考试时间：</label>
		<div class="formControls col-xs-8 col-sm-9">
			<input type="hidden" onfocus="WdatePicker({ maxDate:'#F{$dp.$D(\'logmax\')||\'%y-%M-%d\'}'})" id="logmin" class="input-text Wdate" style="width:120px;" autocomplete="off">
			<input type="text" onfocus="WdatePicker({ minDate:'%y-%M-%d',maxDate:'2050-12-31'})" id="logmax" class="input-text Wdate" style="width:120px;" autocomplete="off">
		</div>
	</div>
	
	<div class="row cl">
		<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
			<input id="submit" onclick=addSchedule() class="btn btn-primary radius" type="button" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
			<button onClick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
		</div>
	</div>
	</form>
	</div>
</div>
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/static/h-ui/js/H-ui.min.js"></script> 
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/static/h-ui.admin/js/H-ui.admin.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/My97DatePicker/4.8/WdatePicker.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/jquery.validation/1.14.0/jquery.validate.js"></script> 
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/jquery.validation/1.14.0/validate-methods.js"></script> 
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/webuploader/0.1.5/webuploader.min.js"></script> 
<script type="text/javascript">
$(function(){
	majorSelect();
	classesSelect();
});
function addSchedule(){ //添加考试安排
	var majorName = $("#majorSelect option:selected").text(); //专业名称
	var classesName = $("#classesSelect option:selected").text(); //班级名称
	var subjectName = $("#subjectSelect option:selected").text(); //专业名称
	var dates = $("#logmax").val(); //考试时间

	if (validate()){
		$.ajax({
			url:"${pageContext.request.contextPath}/ScheduleManage/addSchedule",
			type:"get",
			data:{"majorName":majorName,"subjectName":subjectName,"dates":dates,"classesName":classesName},
			dataType:"json",
			async:false,
			success:function(data){
				if(1==data.res){ //成功
					var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
					parent.layer.close(index); //再执行关闭
					location.reload();
					layer.msg('添加成功',{icon:1,time:2000});
				}
				if(0==data.res){ //失败（已存在）
					layer.msg('该考试安排已存在',{icon:0,time:2000});
				}
			},
			error:function(){
				alert('error');
			}
		});
	}
}
function majorSelect(){ //选择专业 
	$("#majorSelect").change(function(){
		if(0==$("#majorSelect").val()){
			//选择第一个选项，无反应
		} else {
			var majorName = $("#majorSelect option:selected").text();
			//去后台读取该专业下的班级
			$.ajax({
				url:"${pageContext.request.contextPath}/StudentManage/getClassesByMajorName",
				type:"get",
				dataType:"json",
				data:{"majorName":majorName},
				async:false,
				success:function(data){
					//循环遍历填充班级下拉框
					$("#classesSelect").html('');
					$("#classesSelect").append('<option value="0">请选择班级</option>');
					$.each(data,function(index,classes){
						$("#classesSelect").append(
							'<option value='+(index+1)+'>'+classes.classesName+'</option>'
						);
					});
					
					
				
				},
				error:function(){
					alert('请刷新页面');
				}
			}); 		
		}	
	});
}
function classesSelect(){ //选择班级 
	$("#classesSelect").change(function(){
		if("0"==$("#classesSelect option:selected").val()){
			//选择第一个选项，无动作
		} else {
			var majorName = $("#majorSelect option:selected").text();
			var classesName = $("#classesSelect option:selected").text();
			
		}
	});
}


function validate(){ //验证输入的合法性
	var res = true;
	var majorName = $("#majorSelect option:selected").val(); //专业名称
	var classesName = $("#classesSelect option:selected").val(); //班级名称
	var subjectName = $("#subjectSelect option:selected").val(); //专业名称
	var dates = $("#logmax").val(); //考试时间
	
	if(''==dates){
		layer.msg('请选择考试时间',{icon:0,time:2000});
		res = false;
	}
	if('0'==subjectName){
		layer.msg('请选择科目',{icon:0,time:2000});
		res = false;
	}
	if('0'==classesName){
		layer.msg('请选择班级',{icon:0,time:2000});
		res = false;
	}
	if('0'==majorName){
		layer.msg('请选择专业',{icon:0,time:2000});
		res = false;
	}
	return res;
}
</script> 
</body>
</html>